<!--
 * @Autor: 张小鹏
 * @Date: 2020-04-10 15:53:11
 * @LastEditors: 张小鹏
 * @LastEditTime: 2020-04-26 17:01:11
 * @description: 
 -->
<template>
  <div class="ytoorgSelect">
    <cascader
      :options="datalist"
      v-if="isRouterAlive"
      ref="orgSelectcascader"
       :selectvalue="selectvalue"
      :props="orgSelectProps"
      @handleClear="handleClear"
      @change="Handlechange"
      @filterHandler="filterHandler"
      @visible-change="Handlevisible"
      v-bind="{...$attrs}"
 
      filterable
      clearable
      :show-all-levels="false"
    ></cascader>
  </div>
</template>
<script >
import { REGION, YTOZY } from "./common/index";
import { queryOrgSelectFirst, queryOrgSelectNext,queryFuzzyOrg } from "@/api/index";
import { mapGetters } from "vuex";
import cascader from "./cascader/index";
export default {
  components: { cascader },
  computed: {
    orgSelectcascader(){
      return this.$refs.orgSelectcascader;
    }
  },
  props: {
    regionSelect: {
      type: Array,
      default: function() {
        return ["省区"];
      }
    },
    tabInit: {
      type: String,
      default: "prov"
    },
    isSend: {
      type: Boolean,
      default: false //isSend=>false 无权限控制
    },

  },
  data() {
    let baseself = this;
    async function HandlequeryOrgSelect(self, level,node, resolve) {
      console.log("NodeList",node)
      if(level==0)
      {
         let param = {
          region: true,
          type: self.tabInit,
          nowType: self.tabInit,
          send: self.isSend,
          level: level,
          levelCount: self.regionSelect.length
        };
        let resdata = await queryOrgSelectFirst(param);
        console.log("HandlequeryOrgSelect",resdata)
        resolve(resdata);
      }
      else
      {
        if(node.data.leaf)
        {
            resolve([]);
            return false;
        }
          let param = {
                    region: true,
                    code: node.value,
                    type: self.tabInit,
                    nowType: self.tabInit,
                    send:self.isSend,
                    level: level,
                    levelCount: self.regionSelect.length
                }
         let resdata = await queryOrgSelectNext(param);
         resolve(resdata);
      }
    }
    return {
      datalist:[],
      isRouterAlive: true,
      Isfirstload: false,
      selectvalue: [],
      orgSelectvalue:[],
      IsSearch:false,
      orgSelectProps: {
        lazy: true,
        multiple: true,
        label: "name",
        value: "code",
        lazyLoad(node, resolve) {
          const { level } = node;
          console.log("lazyLoadAction",baseself.Isfirstload);
          if (!baseself.Isfirstload) {
             baseself.Isfirstload = true;
             HandlequeryOrgSelect(baseself, level,node, resolve);
          } else {
             HandlequeryOrgSelect(baseself, level,node, resolve);
          }
        }
      }
    };
  },
  mounted() {
    let orgSelect={
        type:this.userOrgType,
        value:[this.userOrgCode],
        name:[this.userOrgName]
    }
    this.$emit('change',orgSelect);
  },
  methods: {
    initData() {},
    Handlechange(values,names) {
      var arr =Array.isArray(values) && values.length===0?[]:values.reduce(function (a, b) { return a.concat(b)} );
      let type=values.join('')==='999999'?'HEAD':this.tabInit;
      this.orgSelectvalue=arr;
      let orgSelect={
        type:type,
        value:arr,
        name:names
      }
      this.$emit('change',orgSelect);
    },
    filterHandler(keyword){
        this.IsSearch=true;
        setTimeout(()=>{
          this.SearchFuzzyOrg(keyword);
        },2000);
    },
   async SearchFuzzyOrg(keyword){
     if(keyword==='')
     {
       this.handleClear();
     }
     else
     {
          let param={
                    "send":this.isSend,
                    "code":keyword,
                    "type":this.tabInit,
                    "userOrgCode":this.userOrgCode,
                    "userOrgType":this.userOrgType
              }
              let resdata=await queryFuzzyOrg(param);
              this.$set(this,'datalist',resdata);
          }

    },
    handleClear(){
      if(this.IsSearch)
      {
        this.IsSearch=false;
        this.datalist=[];
        this.Isfirstload=false;
        this.orgSelectcascader.initStore();
      }
    },
    Handlevisible(visibleflag){
      if(visibleflag && Array.isArray(this.orgSelectvalue) && this.orgSelectvalue.length===0)
      {
        this.handleClear();
      }
    },
     setvalue(val){
       this.$set(this,'selectvalue',val);
    }
  }
};
</script>
<style lang="scss" scoped>
</style>